package com.zrf.geekbang.spring.dependency.lookup;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * 层次性依赖查找demo
 * @author zhouruifeng
 * @date 2020-07-05
 */
public class HierarchicalBeanFactoryDemo {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
        //注入配置类
        applicationContext.register(HierarchicalBeanFactoryDemo.class);

        ConfigurableListableBeanFactory beanFactory = applicationContext.getBeanFactory();
        System.out.println(beanFactory.getParentBeanFactory());
        beanFactory.setParentBeanFactory(createParentBeanFactory());
        System.out.println(beanFactory.getParentBeanFactory());
        //启动应用上下文
        applicationContext.refresh();
    }


    private static BeanFactory createParentBeanFactory() {
        //创建beanFactory容器
        DefaultListableBeanFactory defaultListableBeanFactory = new DefaultListableBeanFactory();
        XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(defaultListableBeanFactory);
        int loadBeanDefinitions = beanDefinitionReader.loadBeanDefinitions("classpath:/META-INF/dependency-lookup-context.xml");
        return defaultListableBeanFactory;
    }
}
